/*
 * COPYRIGHT. ShenZhen iByte Technology Co., Ltd. 2018.
 * ALL RIGHTS RESERVED.
 *
 * No part of this publication may be reproduced, stored in a retrieval system, or transmitted,
 * on any form or by any means, electronic, mechanical, photocopying, recording, 
 * or otherwise, without the prior written permission of ShenZhen iByte Network Technology Co., Ltd.
 *
 * Amendment History:
 * 
 * Date                   By              Description
 * -------------------    -----------     -------------------------------------------
 * Dec 3, 2018    Li.shangzhi         Create the class
 */
package com.github.icloud.elasticsearch.api.aggregations;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.max.Max;
import org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.min.Min;
import org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
import org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder;
import org.junit.Test;

import com.github.icloud.elasticsearch.ElasticsearchClientBase;
/**
 * @FileName MetricsAggregations.java
 * @Description: 
 *
 *	官方文档 @see <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/_metrics_aggregations.html'></a>
 *
 * @Date Dec 3, 2018 5:08:27 PM
 * @author Li.shangzhi
 * @version 1.0
 */
public class MetricsAggregations extends ElasticsearchClientBase{

	@Test
	public void iEsMinAggregation() throws Exception{
		MinAggregationBuilder aggregation =
				AggregationBuilders
				.min("agg")
				.field("age");

		SearchResponse sr = client.prepareSearch("icloud-iot-log")
				.addAggregation(aggregation)
				.get();

		Min aggMin = sr.getAggregations().get("agg");
		String valueMin = aggMin.getValueAsString();	

		System.out.println("min value:" + valueMin);
	}

	@Test
	public void iEsSumAggregation() throws Exception {
//		MaxAggregationBuilder aggregation = 
//				AggregationBuilders
//				.max("agg")
//				.field("age");
//		
//		SearchResponse sResponse = client.prepareSearch("icloud-iot-log")
//				.addAggregation(aggregation)
//				.get();
//		Max aggMax = sResponse.getAggregations().get("agg");
		
		SumAggregationBuilder aggregation = 
				AggregationBuilders
				.sum("agg")
				.field("age");
		
		SearchResponse sResponse = client.prepareSearch("icloud-iot-log")
				.addAggregation(aggregation)
				.get();
		Sum aggSum = sResponse.getAggregations().get("agg");
		
		String valueSum = aggSum.getValueAsString();
		System.out.println("sum value:"+ valueSum);

	}
}
